%FROMQ2ABSORPTION_CMT 通过仿真曲线计算Q值，根据公式计算幅值和相位并于CST结果比较
%@since 2022.4.22
%@update: 调用新函数
%    *参数说明*
%       @param
%       @return
%    *使用说明*
%       直接调用 FROMQ2ABSORPTION_CMT
%
% ———————————————— POWERED BY YINWEI ————————————————

clear,close all;clf;clc;
% 获取Q值 吸收值 和频率
% 非等周期_Dx_小频段 - 1.2-1.25 CMT
[getParametersTable, Abp_contour, Phi] = Spara2QAR_AllInOne([1 4 11 6 7 5], [15 20  24 30 35 40], [1.2 1.25], 'Y', 'CMT', 0, -1);
% 全金属
% [getParametersTable, Abp_contour, Phi] = Spara2QAR_AllInOne([1:18], [-90:5:0], [1.2 1.25], 'Y', 'CMT', 0, -1);
getParameters = table2array(getParametersTable);

%% 计算Qr Qd yr yd
Qr = [getParameters(1:3, 6); getParameters(4:end, 7)];
Qd = [getParameters(1:3, 7); getParameters(4:end, 6)];
% Qr = [getParameters(1:end, 6)];
% Qd = [getParameters(1:end, 7)];
% Qr = [getParameters(1:7, 7); getParameters(8:end, 6)];
% Qd = [getParameters(1:7, 6); getParameters(8:end, 7)];
Freq0 = getParameters(:, 1);
Freq = Abp_contour(:, 1);
gammar = Freq0 * pi ./ Qr;
gammad = Freq0 * pi ./ Qd;
% Q = Qr.*Qd./(Qr+Qd)

YW_Plot_Fast({gammar, gammad})

%%
F_A_X = [];
F_P_PX = [];

for each = 1:size(getParameters, 1)

    w = (Abp_contour(:, 1) - Freq0(each)) * 2 * pi; % 角频率差

    % 吸收率
    X = 4 * gammar(each) .* gammad(each) ./ (w.^2 + (gammar(each) + gammad(each)).^2);
    YW_Plot_Fast({Freq, X, Freq, Abp_contour(:, each + 1)});
    F_A_X = [F_A_X, [Abp_contour(:, each + 1), X]];

    % 用反射系数公式求解相位
    PX = angle((1j * w + gammar(each) - gammad(each)) ./ (-1j * w + gammar(each) + gammad(each)));
    % 相位处理
    PX = unwrap(PX) + 2 * pi;
    % 处理CST的相位，相对于中心相位进行翻转
    NEWPhi = -unwrap(Phi(:, each + 1)) + 2 * 58/180 * pi;
    YW_Plot_Fast({Freq, PX, Freq, NEWPhi}, {'r*-', 'g:'}, ["PX", 'Phi'], ["Fre", 'Phase']);
    F_P_PX = [F_P_PX, [NEWPhi, PX]];
    close all;
end

%     SaveFile([F,F_A_X],'F_A_X','txt');
%     SaveFile([F,F_P_PX],'F_P_PX','txt');
